package com.xnx3.wangmarket.plugin.translate;

import com.xnx3.j2ee.pluginManage.PluginRegister;
import com.xnx3.j2ee.util.SpringUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.bean.NewsDataBean;
import com.xnx3.wangmarket.admin.cache.TemplateCMS;
import com.xnx3.wangmarket.admin.entity.News;
import com.xnx3.wangmarket.admin.entity.SiteColumn;
import com.xnx3.wangmarket.admin.pluginManage.interfaces.TemplateInterface;
import com.xnx3.wangmarket.plugin.translate.entity.Translate;

/**
 * 国际化插件，让你做的网站具有多国语言切换的能力
 * @author 管雷鸣
 */
@PluginRegister(version = "1.4", menuTitle = "国际化",intro="国际化插件，让你做的网站具有多国语言切换的能力", menuHref="/plugin/translate/index.jsp", applyToCMS = true, versionMin="6.0")
public class Plugin implements TemplateInterface{

	@Override
	public BaseVO newsTag(String text, News news, SiteColumn siteColumn, NewsDataBean newsDataBean,
			TemplateCMS template) {
		return null;
	}

	@Override
	public BaseVO publicTag(String text, TemplateCMS template) {
		return null;
	}

	@Override
	public BaseVO publishHtmlFileBefore(String text, String path, TemplateCMS template) {
		if(path.indexOf(".html") == -1) {
			return BaseVO.success(text);
		}
		if(text == null) {
			return BaseVO.success(text);
		}
		
		/**** 是html文件，才会进行处理加入 ***/
		int siteid = template.getSite().getId();
		Translate translate = SpringUtil.getSqlCacheService().findById(Translate.class, siteid);
		if(translate == null) {
			//不存在，那加入一个默认的
			translate = new Translate();
			translate.setId(siteid);
			SpringUtil.getSqlService().save(translate);
			
			//不进行任何操作
			return null;
		}
		if(translate.getEnable() - Translate.ENABLE_QIYONG != 0) {
			//不是启用，直接退出
			return null;
		}
		
		/**** 下面是启用 ****/
		
		if(translate.getJavascript() != null && translate.getJavascript().length() > 0) {
			text = text + "<script src=\"https://cdn.staticfile.net/translate.js/3.8.2/translate.js\"></script>"
					+ "<script>"
					+ "translate.service.use('client.edge'); "
					+ "try{ "+translate.getJavascript()+" }catch(e){ console.log(e); }"
					+ "translate.execute();"
					+ "</script>";
		}
		if(translate.getCss() != null && translate.getCss().length() > 0) {
			text = text + "<style>\n"
					+ translate.getCss()
					+ "\n</style>";
		}
		
		return BaseVO.success(text);
	}

	@Override
	public BaseVO siteColumnTag(String text, SiteColumn siteColumn, TemplateCMS template) {
		return null;
	}
	
}